package com.mall.b2c.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mall.b2c.domain.goods.TagDomain;
import com.mall.b2c.service.TagService;
import com.mall.b2c.util.JsonDataGridHelper;
import com.mall.common.bean.Pager;
import com.mall.common.bean.SearchFilter;
import com.mall.common.controller.BaseController;
import com.mall.common.exception.NoticeException;
import com.mall.common.util.ResponseUtils;
import com.mall.common.validation.annotation.RequiredFieldValidator;
import com.mall.common.validation.annotation.RequiredStringValidator;
import com.mall.common.validation.annotation.Validations;
import com.mall.common.web.Servlets;

/**
 * 标签
 * @author wangzx
 */
@Controller
@RequestMapping("/admin/b2c/tag")
public class TagController  extends BaseController{
	@Resource private TagService tagService;
	
	@RequestMapping("/checkTagName")
	@ResponseBody
	public String checkTagName(String oldName, @RequestParam("name") String newName) {
		if(tagService.isUnique("name", oldName, newName)) {
			return "true";
		} else {
			return "false";
		}
	}
	
	/**
	 * 跳转到列表页面
	 */
	@RequestMapping(value="/list", method=RequestMethod.GET)
	public String list(Model model){
		return "/admin/b2c/tag/list";
	}

	/**
	 * 获取列表信息
	 */
	@RequestMapping(value="/queryData", method=RequestMethod.POST)
	@ResponseBody
	public Pager queryData(Pager pager, HttpServletRequest request){
    	Map<String, Object> filterParams = Servlets.getParametersStartingWith(request);
		List<SearchFilter> filters = SearchFilter.parse(filterParams);
    	pager = tagService.findPage(pager, filters);
		return JsonDataGridHelper.createJSONData(pager);
    }
	
    /**
	 * 跳转到新增页面
	 */
	@RequestMapping(value="/add", method=RequestMethod.GET)
	public String input(Model model){
		return "/admin/b2c/tag/input";
	}
	
	/**
	 * 保存
	 */
	@Validations(
		requiredStrings = {
			@RequiredStringValidator(fieldName = "name", message = "标签名称不允许为空!")
		}
	)
	@RequestMapping(value="/save", method=RequestMethod.POST)
	public String save(TagDomain tag, Model model, HttpServletResponse response){
		if(!tagService.isUnique("name", null, tag.getName())) {
			throw new NoticeException("标签名称已存在!");
		}
		tagService.persist(tag);
		return ResponseUtils.renderJSON(successMessage, response);
    }
    
	/**
	 * 编辑
	 */
	@Validations(
		requiredFields = { 
			@RequiredFieldValidator(fieldName = "id", message = "ID非法!")
		}
	)
	@RequestMapping(value="/edit", method=RequestMethod.GET)
	public String edit(Long id, Model model){
		TagDomain tag = tagService.find(id);
    	model.addAttribute("tag", tag);
		return "/admin/b2c/tag/input";
	}
	
	/**
	 * 修改
	 */
	@Validations(
		requiredStrings = {
			@RequiredStringValidator(fieldName = "name", message = "标签名称不允许为空!")
		}
	)
	@RequestMapping(value="/update", method=RequestMethod.POST)
	@ResponseBody
	public String update(TagDomain tag, HttpServletResponse response){
		TagDomain currentTag = tagService.find(tag.getId());
		if(currentTag == null) {
			throw new NoticeException("当前标签不存在!");
		}
		if(!tagService.isUnique("name", currentTag.getName(), tag.getName())) {
			throw new NoticeException("标签名称已存在!");
		}
		tagService.updateByUpdater(tag);
		return ResponseUtils.renderJSON(successMessage, response);
	}
	
    /**
	 * 删除
	 */
	@Validations(
		requiredFields = {
			@RequiredFieldValidator(fieldName = "ids", message = "ids非法")	
		}
	)
	@RequestMapping(value="/delete", method=RequestMethod.POST)
	@ResponseBody
	public String delete(Long[] ids, HttpServletResponse response){
		tagService.remove(ids);
    	return ResponseUtils.renderJSON(successMessage, response);
    }
}
